package com.inforich.common.utils;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.google.common.base.Splitter;
import com.inforich.common.mybatis.Location;
import com.vividsolutions.jts.geom.Point;
import java.math.BigDecimal;

/**
 * @description: some desc
 * @author: HJM
 * @date: 2022/10/23
 */
public class Utils {

  /**
   * 获取定位
   */
  public static Double[] getLocation(String location) {
    if (StrUtil.isBlank(location)) {
      return new Double[]{118.839087, 31.910599};
    }
    location = StrUtil.subBetween(location, "(", ")");
    String lon = Splitter.on(" ").splitToList(location).get(0).trim();
    String lat = Splitter.on(" ").splitToList(location).get(1).trim();
    return new Double[]{Double.valueOf(lon), Double.valueOf(lat)};
  }

  private static final double EARTH_RADIUS = 6378137;//赤道半径

  private static double rad(double d) {
    return d * Math.PI / 180.0;
  }

  /**
   * 计算距离 米
   */
  public static double getDistance(double lon1, double lat1, double lon2, double lat2) {
    double radLat1 = rad(lat1);
    double radLat2 = rad(lat2);
    double a = radLat1 - radLat2;
    double b = rad(lon1) - rad(lon2);
    double s = 2 * Math.asin(Math.sqrt(
        Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(
            Math.sin(b / 2), 2)));
    s = s * EARTH_RADIUS;
    //单位米
    return new BigDecimal(s).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
  }

  public static void main(String[] args) {
    double distance = getDistance(118.939087, 32.910588, 118.839087, 32.910588);
    System.out.println(distance);
  }
}
